package com.example;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 * @Description:
 * @Author: 赵超
 * @Copyright: Copyright (c) 浙江日报
 * @Date: 2023-05-08 14:05
 * @Version: 1.0
 */
public class StringMethod {


    @Test
    public void testChars() {
        String name = "12345工😃";
        //JDK9+ 获取所有字符 emoji会拆多个
        IntStream chars = name.chars();
        chars.forEach(x -> System.out.println(Character.toChars(x)));
    }

    @Test
    public void testCodePoints() {
        String name = "12345工😃";
        //JDK9+ 获取String中所有char emoji会拆一个
        IntStream chars = name.codePoints();
        chars.forEach(x -> System.out.println(Character.toChars(x)));
    }

    @Test
    public void testDescribeConstable() {
        String name = "12345工😃";
        //JDK12+
        Optional<String> optionalString = name.describeConstable();
        System.out.println(optionalString.get());
    }

    @Test
    public void testFormatted() {
        String name = "12345工😃%s-%d";
        //JDK15+
        String formattedString = name.formatted("admin",1);
        Assertions.assertEquals(String.format(name,"admin",1),formattedString);
    }

    @Test
    public void testIndent() {
        String name = "1\n2\r3\r\n4";
        //JDK11 按行缩进
        String indentString = name.indent(20);
        System.out.println("|" + indentString + "|");
    }

    @Test
    public void testIsBlank() {
        System.out.println("".isBlank());
        System.out.println("".isEmpty());
        System.out.println(" ".isBlank());
        System.out.println(" ".isEmpty());
        System.out.println("\t\n".isBlank());
        System.out.println("\n\n".isEmpty());

    }

    @Test
    public void testLines() {
        String name = "1\n2\r3\r\n4";
        //JDK11+ 按\n\r拆分String
        List<String> lines = name.lines().collect(Collectors.toList());
        lines.forEach(System.out::println);
    }

    @Test
    public void testRepeat() {
        String name = "admin";
        //JDK11+ 重复String
        String jdkRepeatString = name.repeat(3);
        System.out.println(jdkRepeatString);
    }

    @Test
    public void testStrip() {
        //JDK11+
        String name = "\u0020 \t admin\0";
        //JDK11+ strip()是trim(). 含义trim()仅删除字符 <= U+0020（空格）；strip()删除所有 Unicode 空白字符（但不是所有控制字符，例如 \0）
        System.out.println(name.strip() + "|");
        System.out.println(name.trim() + "|");
    }

    @Test
    public void testStripIndent() {
        String name = " admin |\n| admin ";
        //JDK11+
        System.out.println("|"+name.stripLeading()+"|");
        System.out.println("-------------");
        System.out.println("|"+name.stripIndent()+"|");
        System.out.println("-------------");
        System.out.println("|"+name.stripTrailing()+"|");
    }

    @Test
    public void testTranslateEscapes() {
        String name = "\b\r\t\f";
        //JDK15+
        String transform = name.translateEscapes();
        System.out.println(transform);
    }

    @Test
    public void testStringBlock() {
        //JDK8之前想要定义一个JSON数据格式
        String json = "{\n" +
                "    \"name\": \"admin\",\n" +
                "    \"id\": 1\n" +
                "}";
        String json2 = """
                {
                    "name": "admin",
                    "id": 1
                }""";
        //JDK17+
        System.out.println(json);
        System.out.println(json2);
        System.out.println(json.equals(json2));
    }


}
